﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.物料管理
{
    /// <summary>
    /// 物料知识库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMaterialWikiController : ControllerBase
    {
        public TMaterialWikiController(ITMaterialWikiRepository tMaterialWiki)
        {
            TMaterialWiki = tMaterialWiki;
        }

        public ITMaterialWikiRepository TMaterialWiki { get; }

        /// <summary>
        /// 添加物料知识库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTMaterialWiki(TMaterialWiki m)
        {
            bool IsOK = TMaterialWiki.AddTMaterialWiki(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！", "500"));
        }

        /// <summary>
        /// 删除物料知识库
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTMaterialWiki(int mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = TMaterialWiki.DelTMaterialWiki(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));

        }

        /// <summary>
        /// 修改物料知识库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTMaterialWiki(TMaterialWiki m)
        {
            if(m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id不能为0！"));
            }
            bool IsOK = TMaterialWiki.UpdTMaterialWiki(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));

        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialWikiById(int mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = TMaterialWiki.GetTMaterialWikiById(mid);
            if (rule != null)
            {
                return Ok(BaseResult.Result(rule, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！Id可能有误或已被删除！", "500"));

        }

        /// <summary>
        /// 显示物料知识库列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialWikiList(string? mname,int pageIndex=1,int pageSize=10)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = TMaterialWiki.GetTMaterialWikiList(mname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdFile(IFormFile file)
        {
            // 检查文件是否为空
            if (file == null || file.Length == 0)
            {
                return Ok(BaseResult.Result("400", "没有选择文件或文件为空"));
            }
            string filesave = Directory.GetCurrentDirectory() + "\\wwwroot\\file\\" + file.FileName;
            using (FileStream fs = new FileStream(filesave, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(file.FileName);
        }
    }
}
